# .github/workflows/pre-commit.yml
# https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions
---

name: pre-commit

on:
  workflow_dispatch:
  pull_request:
  push:
#    branches: ["master", "main"]
    paths-ignore:
      - '**.md'

jobs:
  pre-commit:
    timeout-minutes: 5
    runs-on: ubuntu-latest
    strategy:
      fail-fast: true
      matrix:
        python-version: [ "3.12" ]

    steps:
    - uses: actions/checkout@v4

    - uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}


    - name: Restore cached python
      id: python-cache
      uses: actions/cache/restore@v4
      with:
        path: |
          ~/.cache/pypoetry
          ~/.local
          .venv
        key: ${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('poetry.lock') }}


    - uses: snok/install-poetry@v1
      if: steps.python-cache.outputs.cache-hit != 'true'
      with:
        virtualenvs-create: true
        virtualenvs-in-project: true
        installer-parallel: true

    - name: Install dependencies
      if: steps.python-cache.outputs.cache-hit != 'true'
      run: |
        poetry install --no-interaction

    - name: Save Python cache
      if: steps.python-cache.outputs.cache-hit != 'true'
      id: python-cache-save
      uses: actions/cache/save@v4
      with:
        path: |
          ~/.cache/pypoetry
          ~/.local
          .venv
        key: ${{ steps.python-cache.outputs.cache-primary-key }}

    - name: Run pre-commit
      run: |
        source .venv/bin/activate
        poetry run pre-commit run --all-files


